package xin.lz1998.cq.plugin.customize;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import xin.lz1998.cq.constant.GroupInfoConstant;
import xin.lz1998.cq.event.message.CQGroupMessageEvent;
import xin.lz1998.cq.retdata.ApiData;
import xin.lz1998.cq.retdata.GroupMemberInfoData;
import xin.lz1998.cq.robot.CQPlugin;
import xin.lz1998.cq.robot.CoolQ;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 成员信息功能
 */
@Component
@Order(4)//插件执行顺序
public class GetFGroupInfoPlugin extends CQPlugin {


    @Override
    public int onGroupMessage(CoolQ cq, CQGroupMessageEvent event) {
        String info="";
        String msg = event.getMessage().trim();
        long groupId = event.getGroupId();
        Long adminId = event.getUserId();
        String[] arr = msg.split(" ");
        ApiData<GroupMemberInfoData> groupMemberInfo = null;
        if(msg.startsWith(GroupInfoConstant.HEARD)){
            if(arr.length==1){
                groupMemberInfo = cq.getGroupMemberInfo(groupId, event.getUserId(), true);
            }else if((arr.length==2)){
                if (GroupInfoConstant.GROUPID == groupId ) {
                    if (arr[1].startsWith("[")) {
                        String[] idArr = arr[1].split("=");
                        Long user_id = Long.valueOf(idArr[1].substring(0, idArr[1].length() - 1));
                        groupMemberInfo = cq.getGroupMemberInfo(groupId, user_id, true);
                    } else {
                        cq.sendGroupMsg(groupId, "用户"+adminId+GroupInfoConstant.INFO, true);
                        return MESSAGE_IGNORE;
                    }
                }
            }else{
                cq.sendGroupMsg(groupId, "用户"+adminId+GroupInfoConstant.INFO1, true);
                return MESSAGE_IGNORE;
            }
        }

        if(null!=groupMemberInfo&&groupMemberInfo.getStatus().equals("ok")){
            GroupMemberInfoData data = groupMemberInfo.getData();

            String sex=data.getSex().equals("male")?GroupInfoConstant.SEX2:GroupInfoConstant.SEX1;

            info+="昵称\t\t"+data.getNickname()+"\n";
            info+="年龄\t\t"+data.getAge()+"\n";
            info+="性别\t\t"+sex+"\n";
            info+="地址\t\t"+data.getArea()+"\n";
            info+="等级\t\t"+data.getLevel()+"\n";
            info+="头衔\t\t"+data.getTitle()+"\n";
            info+="加群时间\t\t"+timestamp2Date((long) data.getJoinTime())+"\n";
            info+="最后一次发言时间\t\t"+timestamp2Date((long) data.getLastSentTime())+"\n";
            info+="\n";

            info+=sex.equals(GroupInfoConstant.SEX1)?info=GroupInfoConstant.HANZI:GroupInfoConstant.MEIZI;
        }
        cq.sendGroupMsg (groupId, info, true);
        return MESSAGE_IGNORE;
    }


    public static String timestamp2Date(Long time){
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(time * 1000));
    }
}




